package com.aifast.dict.expand;

import cn.hutool.core.util.ObjectUtil;
import com.aifast.dict.annotations.DictBind;
import com.aifast.dict.core.FastDictTranslator;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Field;

/**
 * @Author aifast
 * @since 2022/12/31 16:39
 */
public class FastDictEasyExcelConverter implements Converter<Object> {

    @Autowired
    private FastDictTranslator translator;

    @Override
    public Class<?> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Field field = contentProperty.getField();
        DictBind annotation = field.getAnnotation(DictBind.class);
        if(ObjectUtil.isNull(annotation)){
            return new WriteCellData<>(CellDataTypeEnum.STRING,String.valueOf(value));
        }
        Object o = translator.genTargetFieldValue(annotation, value);
        return new WriteCellData<>(CellDataTypeEnum.STRING,String.valueOf(o));
    }
}
